﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AcsCommon;

namespace YiShi.KnowledgePoint
{
    public partial class KnowledgePointEdit : System.Web.UI.Page
    {
        DbAccess db;

        public KnowledgePointEdit()
        {
            db = DbAccess.Get("Admin");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            JsonObject jo = new JsonObject();
            try
            {
                string action = Request["ActionType"] == null ? "" : Request["ActionType"];
                switch (action.ToLower())
                {
                    case "insert":
                        jo = Insert();
                        break;
                    case "update":
                        jo = Update();
                        break;
                    case "updatesortindex":
                        jo = UpdateSortIndex();
                        break;
                    case "delete":
                        jo = Delete();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e1)
            {
                jo.Add("IsSuccess", "false");
                jo.Add("Message", e1.Message);
                jo.Add("Source", e1.Source);
                jo.Add("StackTrace", e1.StackTrace);
            }
            finally
            {
                if (jo.Count > 0)
                {
                    Response.Write(jo.ToString().Replace("'", "\""));
                    Response.End();
                }
            }
        }



        private JsonObject Delete()
        {
            JsonObject jo = new JsonObject();
            string id = this.Request["Id"];
            Class.KnowledgePoint kp = new YiShi.Class.KnowledgePoint(db);
            kp.Id = id;
            kp.Delete();
            jo.Add("IsSuccess", "true");
            return jo;
        }

        private JsonObject UpdateSortIndex()
        {
            JsonObject jo = new JsonObject();
            string id = this.Request["Id"];
            string sortindex = this.Request["SortIndex"];
            Class.KnowledgePoint kp = new YiShi.Class.KnowledgePoint(db);
            kp.FillFromDb(id);
            kp.SortIndex = sortindex;
            kp.Update();
            jo.Add("IsSuccess", "true");
            return jo;
        }

        private JsonObject Update()
        {
            JsonObject jo = new JsonObject();
            string id = this.Request["Id"];
            string name = this.Request["Name"];
            string type = this.Request["Type"];
            Class.KnowledgePoint kp = new YiShi.Class.KnowledgePoint(db);
            kp.FillFromDb(id);
            kp.Name = name;
            kp.Type = type;
            kp.Update();
            jo.Add("IsSuccess", "true");
            return jo;
        }

        private JsonObject Insert()
        {
            JsonObject jo = new JsonObject();
            string id = this.Request["Id"];
            string sortIndex = this.Request["SortIndex"];
            string name = this.Request["Name"];
            string type = this.Request["Type"];
            Class.KnowledgePoint kp = new YiShi.Class.KnowledgePoint(db);
            kp.FillFromDb(id);
            Class.KnowledgePoint childkp = kp.InsertChild(name, type, sortIndex);
            jo.Add("IsSuccess", "true");
            jo.Add("Id", childkp.Id);
            return jo;
        }
    }
}
